package repo

import (
	"errors"
	"storeManageApi/models/dbModels"
	"storeManageApi/models/dbModels/reflectDbModels"
	"storeManageApi/utils"
	"xorm.io/builder"
)

type IInNotesRepo interface {
	AddRow(row dbModels.InNotes) (int64, error)
	GetPage(start, pageSize int) ([]dbModels.InNotes, error)
	GetCount() (int64, error)
}

func CreateInNotesRepo() IInNotesRepo {
	var inNotesTable reflectDbModels.InNotesTable = reflectDbModels.GetInNotesTable()
	var storeNotesTable reflectDbModels.StoreNotesTable = reflectDbModels.GetStoreNotesTable()
	var goodsConfigsTable reflectDbModels.GoodsConfigsTable = reflectDbModels.GetGoodsConfigsTable()
	return &InNotesRepo{
		InNoteTable:       inNotesTable,
		StoreNotesTable:   storeNotesTable,
		GoodsConfigsTable: goodsConfigsTable,
	}
}

type InNotesRepo struct {
	InNoteTable       reflectDbModels.InNotesTable
	StoreNotesTable   reflectDbModels.StoreNotesTable
	GoodsConfigsTable reflectDbModels.GoodsConfigsTable
}

func (repo InNotesRepo) GetCount() (int64, error) {
	return Engin.Table(repo.InNoteTable.Table).Count()
}

func (repo InNotesRepo) GetPage(start, pageSize int) ([]dbModels.InNotes, error) {
	var results []dbModels.InNotes = make([]dbModels.InNotes, 0)
	err := Engin.Table(repo.InNoteTable.Table).Desc(repo.InNoteTable.Base.Id).Limit(pageSize, start-1).Find(&results)
	return results, err
}

func (repo InNotesRepo) AddRow(row dbModels.InNotes) (int64, error) {
	//	1.查询物品配置是否存在
	//2.查询库存是否存在
	session := Engin.NewSession()
	defer session.Close()
	var goods dbModels.GoodsConfigs = dbModels.GoodsConfigs{}
	goodsBool, goodsErr := Engin.Table(repo.GoodsConfigsTable.Table).Where(builder.Eq{
		repo.GoodsConfigsTable.Base.Uid: row.GoodsUid,
	}).Get(&goods)
	if goodsErr != nil {
		return 0, goodsErr
	}
	if !goodsBool {
		return 0, errors.New("查询关联物品出错,请确认物品uid！")
	}

	var store dbModels.StoreNotes = dbModels.StoreNotes{}
	storeBool, storeErr := Engin.Table(repo.StoreNotesTable.Table).Where(builder.Eq{
		repo.StoreNotesTable.GoodsUid: row.GoodsUid,
	}).Get(&store)
	if storeErr != nil {
		return 0, storeErr
	}
	if storeBool {
		store.Count += row.ActionNumber
		if store.Count < 0 {
			return 0, errors.New("库存余额不足，请先购置入库！")
		}
		if store.Count == 0 {
			store.Count = -1
		}
		row.EndCount = store.Count
		var status int = 2
		if store.Count <= store.SafeNumber {
			status = 1
		}
		if store.Count <= 0 {
			status = -1
		}
		store.Status = status
		if err := session.Begin(); err != nil {
			return 0, err
		}
		_, err := session.Table(repo.StoreNotesTable.Table).ID(store.Base.Id).Update(store)
		if err != nil {
			session.Rollback()
			return 0, err
		}
	} else {
		var newStatus int = 2
		if row.ActionNumber <= goods.SafeNumber {
			newStatus = 1
		}
		if row.ActionNumber <= 0 {
			newStatus = -1
		}
		var newStore dbModels.StoreNotes = dbModels.StoreNotes{
			Base: dbModels.Base{
				Uid: utils.RandomUid(),
			},
			GoodsUid:   row.GoodsUid,
			Count:      row.ActionNumber,
			SafeNumber: goods.SafeNumber,
			Status:     newStatus,
		}
		_, err := session.Insert(&newStore)
		row.EndCount = row.ActionNumber
		if err != nil {
			session.Rollback()
			return 0, err
		}
	}
	_, rowErr := session.Insert(&row)
	if rowErr != nil {
		session.Rollback()
		return 0, rowErr
	}
	commitErr := session.Commit()
	if commitErr != nil {
		return 0, commitErr
	}
	return 1, nil
}
